//
//  MLUserInterface.m
//  WaterProof
//
//  Created by victor0321 on 12/1/4.
//  Copyright 2012年 __MyCompanyName__. All rights reserved.
//

#import "MLUserInterface.h"


@implementation MLUserInterface

@synthesize haveWeapon;
@synthesize wantToPlaceWeapon;
@synthesize weaponPlaced;
@synthesize weapon;
@synthesize touchLocation;

@synthesize canPickTubeUpToLeft;
@synthesize canPickBucketBig;
@synthesize canPickTubeUpToRight;
@synthesize canPickBucketSmall;
@synthesize canPickTubeRightToDown;
@synthesize canPickBucketMiddle;
@synthesize canPickTubeVertical;
@synthesize canPickTubeHorizontal;
@synthesize canPickTubeLeftToDown;

static MLUserInterface* uiLayer;

+(MLUserInterface*) sharedLayer{
	NSAssert(uiLayer != nil, @"MLGameLayer not available!");
	return uiLayer;
}

-(id) init
{
	if ((self = [super init]))
	{
		CCLOG(@"===========================================");
        CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self);
        uiLayer = self;
        screenSize = [[CCDirector sharedDirector] winSize];
		
        CCSprite* restart = [CCSprite spriteWithFile:@"btn_restart.png"];
        restart.anchorPoint = CGPointMake(0,1);
        restart.position = CGPointMake(0, screenSize.height);
        [self addChild:restart z:1 tag:RestartBtnTag];
        
        CCSprite* pause = [CCSprite spriteWithFile:@"btn_pause.png"];
        pause.anchorPoint = CGPointMake(0,1);
        pause.position = CGPointMake(0+restart.contentSize.width, screenSize.height);
        [self addChild:pause z:1 tag:PauseBtnTag];
        
        CCSprite* weapon_column = [CCSprite spriteWithFile:@"weapons_column.png"];
        weapon_column.anchorPoint = CGPointMake(0.5, 0);
        weapon_column.position = CGPointMake(screenSize.width/2, 0);
        
        
        
        CCSprite* tubeVertical = [CCSprite spriteWithFile:@"btn_tube_1.png"];
        tubeVertical.position = CGPointMake(174, weapon_column.contentSize.height/2);
        [weapon_column addChild:tubeVertical z:1 tag:TubeVertical];
        CCProgressTimer* maskTubeVertical = [CCProgressTimer progressWithFile:@"btn_weapon_mask.png"];
        maskTubeVertical.position = tubeVertical.position;
        maskTubeVertical.type = kCCProgressTimerTypeVerticalBarTB;
        maskTubeVertical.percentage = 100;
        [weapon_column addChild:maskTubeVertical z:2 tag:MaskTubeVertical];
        maskTubeVertical.visible = NO;
        
        
        CCSprite* tubeHorizontal = [CCSprite spriteWithFile:@"btn_tube_2.png"];
        tubeHorizontal.position = CGPointMake(270, weapon_column.contentSize.height/2);
        [weapon_column addChild:tubeHorizontal z:1 tag:TubeHorizontal];
        CCProgressTimer* maskTubeHorizontal = [CCProgressTimer progressWithFile:@"btn_weapon_mask.png"];
        maskTubeHorizontal.position = tubeHorizontal.position;
        maskTubeHorizontal.type = kCCProgressTimerTypeVerticalBarTB;
        maskTubeHorizontal.percentage = 100;
        [weapon_column addChild:maskTubeHorizontal z:2 tag:MaskTubeHorizontal];
        maskTubeHorizontal.visible = NO;
        
        
        CCSprite* tubeLeftToDown = [CCSprite spriteWithFile:@"btn_tube_3.png"];
        tubeLeftToDown.position = CGPointMake(366, weapon_column.contentSize.height/2);
        [weapon_column addChild:tubeLeftToDown z:1 tag:TubeLeftToDown];
        CCProgressTimer* maskTubeLeftToDown = [CCProgressTimer progressWithFile:@"btn_weapon_mask.png"];
        maskTubeLeftToDown.position = tubeLeftToDown.position;
        maskTubeLeftToDown.type = kCCProgressTimerTypeVerticalBarTB;
        maskTubeLeftToDown.percentage = 100;
        [weapon_column addChild:maskTubeLeftToDown z:2 tag:MaskTubeLeftToDown];
        maskTubeLeftToDown.visible = NO;
        
        
        CCSprite* tubeRightToDown = [CCSprite spriteWithFile:@"btn_tube_4.png"];
        tubeRightToDown.position = CGPointMake(462, weapon_column.contentSize.height/2);
        [weapon_column addChild:tubeRightToDown z:1 tag:TubeRightToDown];
        CCProgressTimer* maskTubeRightToDown = [CCProgressTimer progressWithFile:@"btn_weapon_mask.png"];
        maskTubeRightToDown.position = tubeRightToDown.position;
        maskTubeRightToDown.type = kCCProgressTimerTypeVerticalBarTB;
        maskTubeRightToDown.percentage = 100;
        [weapon_column addChild:maskTubeRightToDown z:2 tag:MaskTubeRightToDown];
        maskTubeRightToDown.visible = NO;
        
        
        CCSprite* tubeUpToRight = [CCSprite spriteWithFile:@"btn_tube_5.png"];
        tubeUpToRight.position = CGPointMake(558, weapon_column.contentSize.height/2);
        [weapon_column addChild:tubeUpToRight z:1 tag:TubeUpToRight];
        CCProgressTimer* maskTubeUpToRight = [CCProgressTimer progressWithFile:@"btn_weapon_mask.png"];
        maskTubeUpToRight.position = tubeUpToRight.position;
        maskTubeUpToRight.type = kCCProgressTimerTypeVerticalBarTB;
        maskTubeUpToRight.percentage = 100;
        [weapon_column addChild:maskTubeUpToRight z:2 tag:MaskTubeUpToRight];
        maskTubeUpToRight.visible = NO;
        
        
        CCSprite* tubeUpToLeft = [CCSprite spriteWithFile:@"btn_tube_6.png"];
        tubeUpToLeft.position = CGPointMake(654, weapon_column.contentSize.height/2);
        [weapon_column addChild:tubeUpToLeft z:1 tag:TubeUpToLeft];
        CCProgressTimer* maskTubeUpToLeft = [CCProgressTimer progressWithFile:@"btn_weapon_mask.png"];
        maskTubeUpToLeft.position = tubeUpToLeft.position;
        maskTubeUpToLeft.type = kCCProgressTimerTypeVerticalBarTB;
        maskTubeUpToLeft.percentage = 100;
        [weapon_column addChild:maskTubeUpToLeft z:2 tag:MaskTubeUpToLeft];
        maskTubeUpToLeft.visible = NO;
        
        
        CCSprite* bucketSmall = [CCSprite spriteWithFile:@"btn_bucket_small.png"];
        bucketSmall.position = CGPointMake(750, weapon_column.contentSize.height/2);
        [weapon_column addChild:bucketSmall z:1 tag:BucketSmall];
        CCProgressTimer* maskBucketSmall = [CCProgressTimer progressWithFile:@"btn_weapon_mask.png"];
        maskBucketSmall.position = bucketSmall.position;
        maskBucketSmall.type = kCCProgressTimerTypeVerticalBarTB;
        maskBucketSmall.percentage = 100;
        [weapon_column addChild:maskBucketSmall z:2 tag:MaskBucketSmall];
        maskBucketSmall.visible = NO;
        
        
        CCSprite* bucketMiddle = [CCSprite spriteWithFile:@"btn_bucket_middle.png"];
        bucketMiddle.position = CGPointMake(846, weapon_column.contentSize.height/2);
        [weapon_column addChild:bucketMiddle z:1 tag:BucketMiddle];
        CCProgressTimer* maskBucketMiddle = [CCProgressTimer progressWithFile:@"btn_weapon_mask.png"];
        maskBucketMiddle.position = bucketMiddle.position;
        maskBucketMiddle.type = kCCProgressTimerTypeVerticalBarTB;
        maskBucketMiddle.percentage = 100;
        [weapon_column addChild:maskBucketMiddle z:2 tag:MaskBucketMiddle];
        maskBucketMiddle.visible = NO;
        
        
        CCSprite* bucketBig = [CCSprite spriteWithFile:@"btn_bucket_big.png"];
        bucketBig.position = CGPointMake(942, weapon_column.contentSize.height/2);
        [weapon_column addChild:bucketBig z:1 tag:BucketBig];
        CCProgressTimer* maskBucketBig = [CCProgressTimer progressWithFile:@"btn_weapon_mask.png"];
        maskBucketBig.position = bucketBig.position;
        maskBucketBig.type = kCCProgressTimerTypeVerticalBarTB;
        maskBucketBig.percentage = 100;
        [weapon_column addChild:maskBucketBig z:2 tag:MaskBucketBig];
        maskBucketBig.visible = NO;
        
        [self addChild:weapon_column z:1 tag:WeaponColumnTag];
        
        haveWeapon = NO;
        wantToPlaceWeapon = NO;
        weaponPlaced = NO;
		self.isTouchEnabled = YES;
        
        canPickTubeHorizontal = YES;
        canPickTubeVertical = YES;
        canPickTubeLeftToDown = YES;
        canPickTubeRightToDown = YES;
        canPickTubeUpToLeft = YES;
        canPickTubeUpToRight = YES;
        canPickBucketBig = YES;
        canPickBucketMiddle = YES;
        canPickBucketSmall = YES;
        
        [self scheduleUpdate];
	}
	return self;
}

-(void) update:(ccTime)delta {
    
    if(weaponPlaced)
    {
        CCSprite* weapon_column = (CCSprite*)[self getChildByTag:WeaponColumnTag];
        switch (weapon) {
            case TubeVertical:
            {
                CCProgressTimer* vertical = (CCProgressTimer*)[weapon_column getChildByTag:MaskTubeVertical];
                [self addChild:[MLCountDownFreezeTime countDownWith:vertical Tag:MaskTubeVertical]];
                break;
            }
            case TubeHorizontal:
            {
                CCProgressTimer* horizontal = (CCProgressTimer*)[weapon_column getChildByTag:MaskTubeHorizontal];
                [self addChild:[MLCountDownFreezeTime countDownWith:horizontal Tag:MaskTubeHorizontal]];
                break;
            }
            case TubeUpToRight:
            {
                CCProgressTimer* upToRight = (CCProgressTimer*)[weapon_column getChildByTag:MaskTubeUpToRight];
                [self addChild:[MLCountDownFreezeTime countDownWith:upToRight Tag:MaskTubeUpToRight]];
                break;
            }
            case TubeUpToLeft:
            {
                CCProgressTimer* upToLeft = (CCProgressTimer*)[weapon_column getChildByTag:MaskTubeUpToLeft];
                [self addChild:[MLCountDownFreezeTime countDownWith:upToLeft Tag:MaskTubeUpToLeft]];
                break;
            }
            case TubeRightToDown:
            {
                CCProgressTimer* rightToDown = (CCProgressTimer*)[weapon_column getChildByTag:MaskTubeRightToDown];
                [self addChild:[MLCountDownFreezeTime countDownWith:rightToDown Tag:MaskTubeRightToDown]];
                break;
            }
            case TubeLeftToDown:
            {
                CCProgressTimer* leftToDown = (CCProgressTimer*)[weapon_column getChildByTag:MaskTubeLeftToDown];
                [self addChild:[MLCountDownFreezeTime countDownWith:leftToDown Tag:MaskTubeLeftToDown]];
                break;
            }
            case BucketSmall:
            {
                CCProgressTimer* bucketSmall = (CCProgressTimer*)[weapon_column getChildByTag:MaskBucketSmall];
                [self addChild:[MLCountDownFreezeTime countDownWith:bucketSmall Tag:MaskBucketSmall]];
                break;
            }
            case BucketMiddle:
            {
                CCProgressTimer* bucketMiddle = (CCProgressTimer*)[weapon_column getChildByTag:MaskBucketMiddle];
                [self addChild:[MLCountDownFreezeTime countDownWith:bucketMiddle Tag:MaskBucketMiddle]];
                break;
            }
            case BucketBig:
            {
                CCProgressTimer* bucketBig = (CCProgressTimer*)[weapon_column getChildByTag:MaskBucketBig];
                [self addChild:[MLCountDownFreezeTime countDownWith:bucketBig Tag:MaskBucketBig]];
                break;
            }
                
            default:
                break;
        }
        weaponPlaced = NO;
    }
}

-(void)placeWeapon:(WeaponMask)theWeapon approved:(BOOL)approve{
    switch (theWeapon) {
        case MaskBucketSmall:
            canPickBucketSmall = approve;
            break;
        case MaskBucketMiddle:
            canPickBucketMiddle = approve;
            break;
        case MaskBucketBig:
            canPickBucketBig = approve;
            break;
        case MaskTubeVertical:
            canPickTubeVertical = approve;
            break;
        case MaskTubeHorizontal:
            canPickTubeHorizontal = approve;
            break;
        case MaskTubeUpToLeft:
            canPickTubeUpToLeft = approve;
            break;
        case MaskTubeUpToRight:
            canPickTubeUpToRight = approve;
            break;
        case MaskTubeLeftToDown:
            canPickTubeLeftToDown = approve;
            break;
        case MaskTubeRightToDown:
            canPickTubeRightToDown = approve;
            break;
        default:
            break;
    }
}

-(void) registerWithTouchDispatcher
{
	[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}

-(BOOL)isTouchFor:(CGPoint)touchPoint Tag:(UIBtnTags)tag {
    CCNode* node = [self getChildByTag:tag];
    return CGRectContainsPoint([node boundingBox], touchPoint);
}

-(BOOL) isTouchFor:(CGPoint)touchPoint weaponTag:(Weapon)tag {
    
    CCNode* nodeWeaponColumn = [self getChildByTag:WeaponColumnTag];
    CCNode* nodeWeapon = [nodeWeaponColumn getChildByTag:tag];

    // center of the node, since it is the child of uiLayer not scene,
    // it's coordinate is compare to (0,0) or the origin of uiLayer (also is (0,0))
    // so we have to manual add the uiLayer's position
    CGPoint position = [nodeWeapon position];
    position.x += ([nodeWeaponColumn position].x - [nodeWeaponColumn contentSize].width/2);
    position.y += [nodeWeaponColumn position].y;
    CGSize size = [nodeWeapon contentSize];
    CGRect rect = CGRectMake(position.x-size.width/2, position.y-size.height/2, size.width, size.height);
    return CGRectContainsPoint(rect, touchPoint);
}

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    
    CGPoint touchPoint = [[MLGameLayer sharedLayer] locationFromTouch:touch];
    
    if([self isTouchFor:touchPoint Tag:PauseBtnTag])
    {
        ccColor4B c = {0,0,0,80};
        MLPauseLayer *pause = [MLPauseLayer sceneWithColor:c];
        [self addChild:pause z:3];
        
        return YES;
    }
    
    if([self isTouchFor:touchPoint Tag:RestartBtnTag])
    {
        [[MLGameLayer sharedLayer] restartStage];
    }
    
        if(!haveWeapon)
        {
            if([self isTouchFor:touchPoint weaponTag:BucketSmall] && [self canPickBucketSmall])
            {
                haveWeapon = YES;
                wantToPlaceWeapon = NO;
                weapon = BucketSmall;
            }
            else if([self isTouchFor:touchPoint weaponTag:BucketMiddle] && [self canPickBucketMiddle])
            {
                haveWeapon = YES;
                wantToPlaceWeapon = NO;
                weapon = BucketMiddle;
            }
            else if([self isTouchFor:touchPoint weaponTag:BucketBig] && [self canPickBucketBig])
            {
                haveWeapon = YES;
                wantToPlaceWeapon = NO;
                weapon = BucketBig;
            }
            else if([self isTouchFor:touchPoint weaponTag:TubeVertical] && [self canPickTubeVertical])
            {
                haveWeapon = YES;
                wantToPlaceWeapon = NO;
                weapon = TubeVertical;
            }
            else if([self isTouchFor:touchPoint weaponTag:TubeHorizontal] && [self canPickTubeHorizontal])
            {
                haveWeapon = YES;
                wantToPlaceWeapon = NO;
                weapon = TubeHorizontal;
            }
            else if([self isTouchFor:touchPoint weaponTag:TubeUpToRight] && [self canPickTubeUpToRight])
            {
                haveWeapon = YES;
                wantToPlaceWeapon = NO;
                weapon = TubeUpToRight;
            }
            else if([self isTouchFor:touchPoint weaponTag:TubeUpToLeft] && [self canPickTubeUpToLeft])
            {
                haveWeapon = YES;
                wantToPlaceWeapon = NO;
                weapon = TubeUpToLeft;
            }
            else if([self isTouchFor:touchPoint weaponTag:TubeRightToDown] && [self canPickTubeRightToDown])
            {
                haveWeapon = YES;
                wantToPlaceWeapon = NO;
                weapon = TubeRightToDown;
            }
            else if([self isTouchFor:touchPoint weaponTag:TubeLeftToDown] && [self canPickTubeLeftToDown])
            {
                haveWeapon = YES;
                wantToPlaceWeapon = NO;
                weapon = TubeLeftToDown;
            }
        }
        
        else if(haveWeapon && ![self isTouchFor:touchPoint Tag:WeaponColumnTag])
        {
            haveWeapon = NO;
            wantToPlaceWeapon = YES;
            touchLocation = touchPoint;
        }
        else if(haveWeapon && [self isTouchFor:touchPoint Tag:WeaponColumnTag])
        {
            haveWeapon = NO;
            wantToPlaceWeapon = NO;
            touchLocation = touchPoint;
        }

    return YES;
}


-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
    
}

-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
    
}

-(void) dealloc
{
	CCLOG(@"===========================================");
    CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self);
	
    uiLayer = nil;
	// don't forget to call "super dealloc"
	[super dealloc];
}

@end
